﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace JNCY.WinAutoComplate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       
        ExcelHelper eh = new ExcelHelper();
        DataHandle dh = new DataHandle();
        DataTable _sourceData;
        Mutex mutex = new Mutex(false, "FSW");
        private void Form1_Load(object sender, EventArgs e)
        {
            // eh.ExcelDir = "cy";
            _sourceData = eh.ReadExcelData();

            if (!string.IsNullOrEmpty(eh.WatchPath))
            {
                FileSystemWatcher p = new FileSystemWatcher(eh.WatchPath);
                p.EnableRaisingEvents = true;
                p.IncludeSubdirectories = false;
                p.NotifyFilter = NotifyFilters.LastWrite;
                p.Changed += new FileSystemEventHandler(p_Changed);             
            }
          //  SetStyle();
        }

        public void SetStyle()
        {
            //设置单元格自动宽
            dgExcelInfo.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            dgExcelInfo.DefaultCellStyle.ForeColor = Color.White;
            dgExcelInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
            //设置字体大小
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridViewCellStyle1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            dgExcelInfo.RowsDefaultCellStyle = dataGridViewCellStyle1;
            dgExcelInfo.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;

            dgExcelInfo.AutoGenerateColumns = false;
            //设置列填充满区域
            dgExcelInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgExcelInfo.AllowUserToResizeColumns = false;
            dgExcelInfo.AllowUserToResizeRows = false;

           // dgExcelInfo.BackImage = Image.FromFile(@"C:\Users\Administrator\Desktop\网格背景.jpg");
            //设置cell背景透明
            dgExcelInfo.ColumnHeadersDefaultCellStyle.BackColor = Color.White;
           // dgExcelInfo.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            dgExcelInfo.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //加上这句前面的才起作用
            dgExcelInfo.EnableHeadersVisualStyles = false;

            dgExcelInfo.DefaultCellStyle.BackColor = Color.Transparent;
            dgExcelInfo.DefaultCellStyle.ForeColor = Color.White;
            dgExcelInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void p_Changed(object sender, FileSystemEventArgs e)
        {
            try
            {
                FileSystemWatcher fw = sender as FileSystemWatcher;
                fw.EnableRaisingEvents = false;
                fw.EnableRaisingEvents = true;

                mutex.WaitOne();
                Thread.Sleep(500);
                _sourceData = eh.ReadExcelData();

                mutex.ReleaseMutex();
            }
            catch
            {
                MessageBox.Show("检测到目标文件已发生变更，请重新运行软件");
            }
        }

        private void txtExcelSearch_KeyUp(object sender, KeyEventArgs e)
        {     
            dh.Source = _sourceData;
            dh.Handle(txtExcelSearch.Text.Trim());
          
            dgExcelInfo.DataSource = dh.Result;
        }
    }
}
